<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="责任机构" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="name">
              <a-input v-model="model.name" placeholder="请输入责任机构"  ></a-input>
            </a-form-model-item>
          </a-col>
            <a-col :span="24">
            <a-form-model-item label="适用地区" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="areaShow">
               <a-cascader
                v-model="model.areaShow"
                placeholder="请输入适用地区"
                :options="cityData"
              change-on-select
              :field-names="{ label: 'label', value: 'label',children:'children' }"
               
              />
            </a-form-model-item>
          </a-col>
      
          <!-- <a-col :span="24">
            <a-form-model-item label="乐观锁" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="version">
              <a-input-number v-model="model.version" placeholder="请输入乐观锁" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="创建人" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="createUser">
              <a-input v-model="model.createUser" placeholder="请输入创建人"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="创建人账号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="createUserId">
              <a-input v-model="model.createUserId" placeholder="请输入创建人账号"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="修改人" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="lastUser">
              <a-input v-model="model.lastUser" placeholder="请输入修改人"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="修改账号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="lastUserId">
              <a-input v-model="model.lastUserId" placeholder="请输入修改账号"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="修改时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="lastTime">
              <j-date placeholder="请选择修改时间" v-model="model.lastTime"  style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="删除标识" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="isDel">
              <a-input v-model="model.isDel" placeholder="请输入删除标识"  ></a-input>
            </a-form-model-item>
          </a-col> -->
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'

  export default {
    name: 'PolicyMechanismForm',
    components: {
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        cityData:[],
        model:{
         },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
           name: [
              { required: true, message: '请输入责任机构!'},
           ],
           isDel: [
              { required: true, message: '请输入删除标识!'},
           ],
        },
        url: {
          add: "/policy/policyMechanism/add",
          edit: "/policy/policyMechanism/edit",
          queryById: "/policy/policyMechanism/queryById",
          areaInfo:'/manage/codeInfo/areaInfo',
        }
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
    },
    methods: {
      add () {
        this.edit(this.modelDefault);
      },
      edit (record) {
        record.areaShow = !!record.area ? record.area.split("/"):[];
        this.model = Object.assign({}, record);
        this.getArea();
        this.visible = true;
      },
      getArea(){
         getAction(this.url.areaInfo, {  pageNo: 1, pageSize: -1 }).then(res => {
        if (res.success) {
          this.cityData= res.result
        }
      })
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            that.model.area = !!that.model.areaShow && that.model.areaShow.length>0 ? that.model.areaShow.join("/"):'';
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }
         
        })
      },
    }
  }
</script>